$.ajaxSetup ({
    cache: false
});
/**
 * 磁块下方的提示层
 */
function metro_notice_layout()
{
	$('.metro-block').each(function(i) {
		_this  = $('.metro-block').eq(i);
		notice = _this.attr('notice');
		if (typeof notice != 'undefined') {
			html   = '<div class="notice-text">' + notice + '</div><div class="notice-layout"></div>';
			_this.append(html);
		}
	})

	$('.metro-block').on('mouseenter', function() {
		$(this).children('.notice-layout').animate({bottom : 0}, 150);
		$(this).children('.notice-text').animate({bottom : 0}, 150);
	}).on('mouseleave', function() {
		$(this).children('.notice-layout').animate({bottom : '-25px'}, 150);
		$(this).children('.notice-text').animate({bottom : '-25px'}, 150);
	});
}

function dp_menu(id) {
	$(id).mouseover(function(){
		$(id).children('.dp_menu').show();
	}).mouseout(function(){
		$(id).children('.dp_menu').hide();
	});
}

var top_menu = function(url, url2)
{
	$.ajax({
		url : url+'menu',
		dataType : 'text',
		type : 'GET',
		data : {u:url, u2: url2 || ''},
		success : function(data) {
			$('#home').after(data);
		}
	});
}

var content_load = function(url, url2)
{
	url = url || url2;
	$('#main_content').load(url2);
	top_menu(url, url2);
}

var cload = function(url)
{
	$('#main_content').load(url);
	top_menu(url);
}

function save(uri, formName, sb)
{
	form    = document.forms[formName];
	element = form.elements;
	var datas= '';
	for (var i = 0; i <= element.length - 1; i++) {
		if (element[i].type == 'radio' && element[i].getAttribute('checked') == 'checked')
		{
			datas += element[i].name + '=' + element[i].value + '&' ;
		} else if (element[i].type != 'radio') {
			if (element[i].type == 'checkbox' && element[i].getAttribute('checked') == 'checked') {
				element[i].value = 1;
			} else if(element[i].type == 'checkbox' && element[i].getAttribute('checked') != 'checked') {
				element[i].value = 0;
			}
			if (element[i].getAttribute('v') == 're' && element[i].value == '') {
				dpAlert(this, {
					callback : 'alerts',
					height : '230',
					title:'提示',
					content:element[i].getAttribute('msg')
				});
				element[i].focus();
				return false;
			}

			if (typeof element[i].getAttribute('reg') == 'string') {
				var reg = element[i].getAttribute('reg');
				var r   = '';
				switch (reg) {
					case 'd+':
						r = /^\d+$/i;
						break;
					case 'w+':
						r = /^\w+$/i;
						break;
					case 'email' :
						r = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
						break;
					case 'az' :
						r = /^[a-z]+$/i;
						break;
					case 'url' :
						r = /^http:\/\//;
						break;
					case 'phone' :
						r = /^(1)[0-9]{10}$/;
						break;
				}
				if (!r.test(element[i].value)) {
					dpAlert(this, {
						callback : 'alerts',
						height : '230',
						title:'提示',
						content:element[i].getAttribute('regmsg')
					});
					element[i].focus();
					return false;
				}
			}

			datas += element[i].name + '=' + element[i].value + '&' ;
		}
	}
	datas = datas.substr(0, datas.length - 1);
	$.ajax({
		url : uri,
		dataType : 'json',
		type : 'POST',
		data : datas,
		success : function (data) {
			if (data.code == 0) {
				dpAlert(this, {
					callback : 'alerts',
					height : '230',
					title:'提示',
					content:data.msg
				});
			} else if (data.code == 1) {
				dpAlert(this, {
					callback : 'notice',
					content  : data.msg
				});
				$('.top_button').remove();
				cload(data.url);
			} else if (data.code == 2) {
				dpAlert(this, {
					callback : 'aop',
					height : '230',
					title:'提示',
					content:data.msg,
					link_1 : data.link_1,
					text_1 : data.text_1,
					link_2 : data.link_2,
					text_2 : data.text_2
				});
				$('#jump1').click(function(){
					$('#main_content').load(data.link_1);
					$('#edui_fixedlayer').remove();
				});
				$('#jump2').click(function(){
					menu(data.link_2);
				})
			}
		}
	});
}

function create(u)
{
	$('.top_button').remove();
	content_load(u, u);
}

function goback(u, u2)
{
	$('.top_button').remove();
	content_load(u, u2);
}

function select_all(t, n)
{
	var nc = $(t).parent().parent().children('.'+n);
	if($(t).is(':checked')) {
		for (var i = 0; i < nc.length; i++) {
			nc.children('input').attr('checked', 'checked');
		}
	} else {
		for (var i = 0; i < nc.length; i++) {
			nc.children('input').removeAttr('checked');
		}
	}
}

$('input[type=checkbox]').live('click', function(){
	if($(this).is(':checked')) {
		$(this).attr('checked', 'checked').val(1);
	} else {
		$(this).removeAttr('checked').val(0);
	}
})

ajaxcall = function(url, fc)
{
	$.getJSON(url, function(data){
		if (data.code != 0) {
			fc(data);
		} else {
			fc(data);
			dpAlert(this, {
				callback : 'alerts',
				title  : '提示',
				height : '230',
				content:data.msg
			});
		}
	});
}